package com.imti.mall.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imti.mall.model.order.pojo.OmsOrderSetting;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

@Mapper
@Transactional
public interface OrderSettingMapping extends BaseMapper<OmsOrderSetting> {
    /**
     * 获取订单设置
     */
    @Select("SELECT * FROM oms_order_setting " +
            "WHERE id=1")
    OmsOrderSetting getSetting();

    /**
     * 将过期设置保存到数据库
     */
    @Update("UPDATE oms_order_setting\n" +
            "SET\n" +
            "flash_order_overtime=60,\n" +
            "normal_order_overtime=120,\n" +
            "confirm_overtime=15,\n" +
            "finish_overtime=7,\n" +
            "comment_overtime=8\n" +
            "WHERE id=1" )
    int addOrderSetting(OmsOrderSetting omsOrderSetting);

    /**
     * 查询未付款的秒杀订单的id
     */
    @Select("SELECT id FROM oms_order AS oo\n" +
            "WHERE oo.status=0\n" +
            "AND oo.order_type=1\n" +
            "AND NOW()-oo.create_time>=(SELECT flash_order_overtime*60 FROM oms_order_setting WHERE id=1)")
    int[] findFlashNotPayId();

    /**
     * 将未付款的秒杀订单关闭
     */
    @Update("UPDATE oms_order AS oo\n" +
            "SET \n" +
            "STATUS=4,\n" +
            "oo.modify_time=NOW()\n" +
            "WHERE id=#{id}")
    int closeFlashOrder(int id);

    /**
     * 将未付款的秒杀订单关闭写入操作表
     */
    @Insert("INSERT INTO \n" +
            "oms_order_operate_history\n" +
            "(order_id,operate_man,create_time,order_status,note)\n" +
            "VALUES\n" +
            "(#{id},'系统',NOW(),4,'秒杀订单超时未付款，自动关闭')")
    int setFlashNotPayHistory(int id);

    /**
     * 查询未付款的正常订单的id
     */
    @Select("SELECT id FROM oms_order AS oo\n" +
            "WHERE oo.status=0\n" +
            "AND oo.order_type=0\n" +
            "AND NOW()-oo.create_time>=(SELECT flash_order_overtime*60 FROM oms_order_setting WHERE id=1)")
    int[] findNomalNotPayId();

    /**
     * 将未付款的正常订单关闭
     */
    @Update("UPDATE oms_order AS oo\n" +
            "SET \n" +
            "STATUS=4,\n" +
            "oo.modify_time=NOW()\n" +
            "WHERE id=#{id}")
    int closeNormalOrder(int id);

    /**
     * 将未付款的正常订单关闭写入操作表
     */
    @Insert("INSERT INTO \n" +
            "oms_order_operate_history\n" +
            "(order_id,operate_man,create_time,order_status,note)\n" +
            "VALUES\n" +
            "(#{id},'系统',NOW(),4,'订单超时未付款，自动关闭')")
    int setNormalNotPayHistory(int id);


    /**
     * 查询超时未收货的正常订单的id
     */
    @Select("SELECT id FROM oms_order AS oo\n" +
            "WHERE oo.status=2 \n" +
            "AND NOW()-oo.`delivery_time`>=(SELECT flash_order_overtime*60*60*24 FROM oms_order_setting WHERE id=1)")
    int[] findNotGet();

    /**
     * 将未收货的正常订单完成
     */
    @Update("UPDATE oms_order AS oo\n" +
            "SET \n" +
            "STATUS=4,\n" +
            "oo.modify_time=NOW()\n" +
            "WHERE id=#{id}")
    int finishOrder(int id);

    /**
     * 将超时未收货的订单收货写入操作表
     */
    @Insert("INSERT INTO \n" +
            "oms_order_operate_history\n" +
            "(order_id,operate_man,create_time,order_status,note)\n" +
            "VALUES\n" +
            "(#{id},'系统',NOW(),4,'已发货订单自动完成')")
    int setHistoryNotGet(int id);



}
